import { deptList , deptAdd ,deptUpdate} from "@/services/deptMsg";
import {
  ModalForm,
  ProFormRadio,
  ProFormText,
  ProFormTreeSelect
} from "@ant-design/pro-components";
import { Button, Col, Form, Row, message } from "antd";
import React, { useImperativeHandle, useState } from "react";

const ItemOption = React.forwardRef((props: any, ref) => {
  const { refreshList } = props;
  const [form] = Form.useForm<any>();
  const [modalOpen, setModalOpen] = useState(false);
  const [modItem, setModItem] = useState<any>(null);
  const handleModalShow = async (row?: any) => {
    setModItem(row || null);
    if(!modalOpen){
      form.resetFields()  
    }
    if (row) {
      form.setFieldsValue({
        ...row,
        parentId: row.parentId != "0" ? row.parentId : null
      });
    }
    setModalOpen(!modalOpen);
  };
  useImperativeHandle(ref, () => ({
    handleModalShow,
  }));
  return (
    <ModalForm
      title={modItem?.id ? "修改科室" : "新增科室"}
      width={600}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnHidden: true,
        onCancel: handleModalShow,
        centered: true,
        maskClosable: false,
      }}
      open={modalOpen}
      submitter={{
        render: (props) => {
          let optBtns: any = [
            <Button
              className="base-button"
              key="cancel"
              onClick={handleModalShow}
            >
              取消
            </Button>,
            <Button
              className="base-button"
              type="primary"
              key="submit"
              onClick={() => props.form?.submit?.()}
            >
              确定
            </Button>,
          ];
          return optBtns;
        },
      }}
      onFinish={async (values) => {
        const sendData = {
          ...values,
          parentId: values.parentId || "0",
        };
        let res = null;
        if (modItem?.deptId) {
          //编辑
          res = await deptUpdate({
            ...sendData,
            deptId: modItem.deptId,
          });
        } else {
          //添加
          res = await deptAdd(sendData);
        }
        if (res?.code === 200) {
          message.success("操作成功");
          refreshList();
          setModalOpen(false);
        }
        return true;
      }}
    >
      <Row gutter={20}>
        <Col span={24}>
          <ProFormTreeSelect
            name="parentId"
            label="上级科室"
            placeholder="请选择上级科室"
            allowClear
            request={async () => {
              const res = await deptList({});
              if (res?.code === 200) {
                return res.data;
              } else {
                return [];
              }
            }}
            fieldProps={{
              filterTreeNode: true,
              treeDefaultExpandAll: true,
              fieldNames: {
                label: "deptName",
                value: "deptId",
              },
            }}
          />
        </Col>
      </Row>
      <Row gutter={20}>
        <Col span={24}>
          <ProFormText
            name="deptName"
            label="科室名称"
            fieldProps={{
              maxLength: 100,
              showCount: true,
            }}
            rules={[
              {
                required: true,
                message: "请输入科室名称",
              },
            ]}
          />
        </Col>
      </Row>
      <Row gutter={20}>
        <Col span={24}>
          <ProFormRadio.Group
            name="status"
            label="科室状态"
            initialValue={"1"}
            rules={[
              {
                required: true,
                message: "请选择科室状态",
              },
            ]}
            options={[
              {
                label: "已启用",
                value: "1",
              },
              {
                label: "已停用",
                value: "0",
              },
            ]}
            fieldProps={{
              className: "custom-radio-item1",
            }}
          />
        </Col>
      </Row>
    </ModalForm>
  );
});

export default ItemOption;
